﻿<%@ WebHandler Language="C#" Class="updateSlider" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间

public class updateSlider : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.Write(formDataUpload(context));
    }
    private static string formDataUpload(HttpContext context)
    {
        try
        {
            HttpFileCollection postedFile = context.Request.Files;
            bool fileUploaded = false;
            string name = context.Request.Form["name"];
            string mid = context.Request.Form["mid"];
            string intro = context.Request.Form["intro"];
            string imgPath = "";

            string msg = string.Empty;
            string error = string.Empty;

            // "++"

            if (name == "" || intro == "")
            {
                return "{\"error\":\"2\", \"msg\":\"表单项不能为空，请重新填写！\"}";
            }

            if (postedFile.Count > 0)
            {
                string savepath = "";
                string tempPath = "";
                tempPath = System.Web.HttpContext.Current.Server.MapPath("..\\upload\\");//System.Configuration.ConfigurationManager.AppSettings["FolderPath"];  
                savepath = tempPath; //context.Server.MapPath(tempPath);  

                string del_sql = "select s_image  from sliders where id =" + mid + "";
                DataSet delDs = DBHelper.sqlMethod(del_sql);


                for (int j = 0; j < delDs.Tables[0].Rows.Count; j++)
                {
                    System.IO.File.Delete(context.Server.MapPath("~/" + delDs.Tables[0].Rows[j]["s_image"].ToString() + ""));
                }
                
                
                string filename = postedFile[0].FileName;
                if (!String.IsNullOrEmpty(filename))
                {
                    string sExtension = filename.Substring(filename.LastIndexOf('.'));

                    if (!Directory.Exists(savepath))
                        Directory.CreateDirectory(savepath);

                    // string sNewFileName = filename.Substring(0, filename.IndexOf("."));


                    string sNewFileName = Guid.NewGuid().ToString("N");
                    imgPath = @"upload/" + sNewFileName + sExtension;
                    postedFile[0].SaveAs(savepath + @"\" + sNewFileName + sExtension);
                    msg = " 成功! ";
                    error = "0";
                    fileUploaded = true;
                }
               

            }
            else
            {
                msg = "没有选择文件";
                error = "1";
                return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
            }
            
            string sql = " update sliders set s_name ='" + name + "' ,s_intro ='" + intro + "'";
            if (imgPath != "")
            {
                sql += ",s_image = '" + imgPath + "' ";
            }
            sql += " where id = " + mid + " ";
            if (fileUploaded)
            {
                DBHelper.sqlMethodNoDataset(sql);
            }
            
            return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
        }
        catch (Exception e)
        {
            return "{\"error\":\"2\", \"msg\":\"发生错误，请重试\"}";
        }



    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}